<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<!--<input type="hidden" name="rowId">-->
<div class="layuimini-container">

  <div class="layuimini-main">
    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    <input type="hidden" name="hiddenFieldForTable" id="hiddenFieldForTable">
    <script type="text/html" id="currentTableBar">
      <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit"  lay-event="passBtn">通过</a>
      <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="rejectBtn">驳回</a>
    </script>

  </div>
</div>
<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../api/common.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: local + 'approve/page/list',
            method: "post",
            contentType: 'application/json;charset=UTF-8',
            headers: {'Authorization': `Bearer ${ localStorage.getItem("token") }`},
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: 'numbers', title: '序号', width: 60, fixed: 'left' },
                {field: 'name', title: '标题', width: 220},
                {field: 'moduleName', title: '所属栏目'},
                {field: 'updateDate', title: '时间'},
                {field: 'userName', title: '作者'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            unresize: false,
            skin: 'line',
            parseData: function (res) {
                return {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.message,
                    "count": res.data.total,
                    "data": res.data.records
                };
            },
        });

        // 监听工具条
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data; // 获取当前行的数据
            var layEvent = obj.event; // 获取当前操作的事件

            if (layEvent === 'passBtn') { // 通过操作
                passItem(data.id);
            } else if (layEvent === 'rejectBtn') { // 驳回操作
                rejectItem(data.id);
            }
        });

        function passItem(id) {
            $.ajax({
                type: 'POST',
                url: local + 'approve/pass',
                data: JSON.stringify({ id: id }),
                dataType: "json",
                headers: {
                    'Authorization': `Bearer ${ localStorage.getItem("token") }`,
                },
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    if (res.code === 200) {
                        // 刷新当前表格
                        table.reload('currentTableId');
                        layer.msg('通过成功！', { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 0 });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                }
            });
        }

        function rejectItem(id) {
            $.ajax({
                type: 'POST',
                url: local + 'approve/reject',
                data: JSON.stringify({ id: id }),
                dataType: "json",
                headers: {
                    'Authorization': `Bearer ${ localStorage.getItem("token") }`,
                },
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    if (res.code === 200) {
                        // 刷新当前表格
                        table.reload('currentTableId');
                        layer.msg('驳回成功！', { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 0 });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                }
            });
        }


    });
</script>


</body>
</html>
